package com.esreport.api;

import com.alibaba.fastjson.JSONObject;
import com.esreport.Utils.DateUtil;
import com.esreport.Utils.ResultJson;
import com.esreport.dto.Action;
import com.esreport.service.ReportService;
import com.esreport.service.XOtherSourceService;
import com.esreport.service.XReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * RealTimeApi
 *
 * @author cexy
 * @description:
 * @date 2019/6/5
 * @time 10:54
 */
@Slf4j
@RestController
@RequestMapping("realtime/report")
public class RealTimeApi {

    @Autowired
    private ReportService reportService;
    @Autowired
    private XReportService XReportService;
    @Autowired
    private XOtherSourceService xOtherSourceService;

    @RequestMapping(value = "/today", produces = "application/json; charset=utf-8")
    public Object today(Integer siteId) {
        Date start = DateUtil.getDayBegin();
        Date end = DateUtil.getDayEnd();
        try {
            long dayHits = reportService.countHits(start, end, siteId);
            long dayNewUserCount = reportService.countNewUser(start, end, siteId);
            long dayBrowseNewsCount = reportService.countBrowse(start, end, siteId);
            long dayStayTimeSum = reportService.sumStayTime(start, end, siteId);
            long dayAvgStayTime = XReportService.countAvgStayTime(start, end, siteId);
            long dayShareCount = reportService.countShare(start, end, siteId);
            long dayFirstStartCount = XReportService.countFirstStart(start, end, siteId);
            long dayStartCount = XReportService.countStart(start, end, siteId);
            JSONObject json = new JSONObject();
            json.put("todayHits", dayHits);
            json.put("todayNewUser", dayNewUserCount);
            json.put("todayBrowseNewsCount", dayBrowseNewsCount);
            json.put("todayStayTimeSum", dayStayTimeSum);
            json.put("todayAvgStayTime", dayAvgStayTime);
            json.put("todayShareCount", dayShareCount);
            json.put("todayFirstStartCount", dayFirstStartCount);
            json.put("todayStartCount", dayStartCount);
            return ResultJson.successResult("请求成功", json);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("服务器异常");
        }
    }

    @RequestMapping(value = "/week", produces = "application/json; charset=utf-8")
    public Object week(Integer siteId) {
        Date start = DateUtil.getBeginDayOfWeek();
        Date end = DateUtil.getEndDayOfWeek();
        try {
            long weekHits = reportService.countHits(start, end, siteId);
            long weekNewUserCount = reportService.countNewUser(start, end, siteId);
            long weekBrowseNewsCount = reportService.countBrowse(start, end, siteId);
            long weekStayTimeSum = reportService.sumStayTime(start, end, siteId);
            long weekAvgStayTime = XReportService.countAvgStayTime(start, end, siteId);
            long weekShareCount = reportService.countShare(start, end, siteId);
            long weekFirstStartCount = XReportService.countFirstStart(start, end, siteId);
            long weekStartCount = XReportService.countStart(start, end, siteId);
            JSONObject json = new JSONObject();
            json.put("weekHits", weekHits);
            json.put("weekNewUser", weekNewUserCount);
            json.put("weekBrowseNewsCount", weekBrowseNewsCount);
            json.put("weekStayTimeSum", weekStayTimeSum);
            json.put("weekAvgStayTime", weekAvgStayTime);
            json.put("weekShareCount", weekShareCount);
            json.put("weekFirstStartCount", weekFirstStartCount);
            json.put("weekStartCount", weekStartCount);
            return ResultJson.successResult("请求成功", json);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("服务器异常");
        }
    }

    @RequestMapping(value = "/month", produces = "application/json; charset=utf-8")
    public Object month(Integer siteId) {
        Date start = DateUtil.getBeginDayOfMonth();
        Date end = DateUtil.getEndDayOfMonth();
        try {
            long monthHits = reportService.countHits(start, end, siteId);
            long monthNewUserCount = reportService.countNewUser(start, end, siteId);
            long monthBrowseNewsCount = reportService.countBrowse(start, end, siteId);
            long monthStayTimeSum = reportService.sumStayTime(start, end, siteId);
            long monthAvgStayTime = XReportService.countAvgStayTime(start, end, siteId);
            long monthShareCount = reportService.countShare(start, end, siteId);
            long monthFirstStartCount = XReportService.countFirstStart(start, end, siteId);
            long monthStartCount = XReportService.countStart(start, end, siteId);
            JSONObject json = new JSONObject();
            json.put("monthHits", monthHits);
            json.put("monthNewUser", monthNewUserCount);
            json.put("monthBrowseNewsCount", monthBrowseNewsCount);
            json.put("monthStayTimeSum", monthStayTimeSum);
            json.put("monthAvgStayTime", monthAvgStayTime);
            json.put("monthShareCount", monthShareCount);
            json.put("monthFirstStartCount", monthFirstStartCount);
            json.put("monthStartCount", monthStartCount);
            return ResultJson.successResult("请求成功", json);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("服务器异常");
        }
    }

    @RequestMapping(value = "/all", produces = "application/json; charset=utf-8")
    public Object all(Integer siteId) {
        try {
            long allHits = reportService.countHits(siteId);
            long allNewUserCount = xOtherSourceService.countUser(siteId.toString());
            long allBrowseNewsCount = XReportService.countBrowseNews(siteId);
            long allStayTimeSum = XReportService.sumStayTime(siteId);
            long allAvgStayTime = XReportService.countAvgStayTime(siteId);
            long allShareCount = XReportService.countShare(siteId);
            long allFirstStartCount = XReportService.countFirstStart(siteId);
            long allStartCount = XReportService.countStart(siteId);
            JSONObject json = new JSONObject();
            json.put("allHits", allHits);
            json.put("allNewUser", allNewUserCount);
            json.put("allBrowseNewsCount", allBrowseNewsCount);
            json.put("allStayTimeSum", allStayTimeSum);
            json.put("allAvgStayTime", allAvgStayTime);
            json.put("allShareCount", allShareCount);
            json.put("allFirstStartCount", allFirstStartCount);
            json.put("allStartCount", allStartCount);
            return ResultJson.successResult("请求成功", json);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("服务器异常");
        }
    }

    //按照时间维度统计新用户
    @RequestMapping("/groupDayForNewUser")
    public Object groupDayForNewUser(String startTime, String endTime, Integer siteId) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = sf.parse(startTime);
            Date end = sf.parse(endTime);

            Map<String, Long> map = reportService.groupDayForActionByAction(DateUtil.getDayStartTime(start),
                    DateUtil.getDayEndTime(end), siteId, Action.FIRST_START);
            return ResultJson.successResult("请求成功", map);
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("请求失败");
        }
    }

    @RequestMapping("/groupHourForNewUser")
    public Object groupHourForNewUser(String startTime, String endTime, Integer siteId) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = sf.parse(startTime);
            Date end = sf.parse(endTime);

            Map<String, Long> map = reportService.groupHourForActionByAction(DateUtil.getDayStartTime(start),
                    DateUtil.getDayEndTime(end), siteId, Action.FIRST_START);
            return ResultJson.successResult("请求成功", map);
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("请求失败");
        }
    }

    //按照时间维度统计总启动用户
    @RequestMapping("/groupDayForStarUptUser")
    public Object groupDayForStarUptUser(String startTime, String endTime, Integer siteId) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = sf.parse(startTime);
            Date end = sf.parse(endTime);

            Map<String, Long> map = reportService.groupDayForActionByAction(DateUtil.getDayStartTime(start),
                    DateUtil.getDayEndTime(end), siteId, Action.STARTUP);
            return ResultJson.successResult("请求成功", map);
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("请求失败");
        }
    }

    @RequestMapping("/groupHourForStarUptUser")
    public Object groupHourForStarUptUser(String startTime, String endTime, Integer siteId) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = sf.parse(startTime);
            Date end = sf.parse(endTime);

            Map<String, Long> map = reportService.groupHourForActionByAction(DateUtil.getDayStartTime(start),
                    DateUtil.getDayEndTime(end), siteId, Action.STARTUP);
            return ResultJson.successResult("请求成功", map);
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("请求失败");
        }
    }

    //按照时间维度统计总启动用户
    @RequestMapping("/groupDayForCountUser")
    public Object groupDayForCountUser(String startTime, String endTime, Integer siteId) {
        long allNewUserCount = xOtherSourceService.countUser(siteId.toString());
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = sf.parse(startTime);
            Date end = sf.parse(endTime);

            Map<String, Long> map = reportService.groupDayForActionByAction(DateUtil.getDayStartTime(start),
                    DateUtil.getDayEndTime(end), siteId, Action.FIRST_START);
            ListIterator<Map.Entry<String, Long>> li = new ArrayList<>(map.entrySet()).listIterator(map.size());
            while (li.hasPrevious()) {
                Map.Entry<String, Long> entry = li.previous();
                allNewUserCount -= entry.getValue();
                map.put(entry.getKey(), allNewUserCount);
            }
            return ResultJson.successResult("请求成功", map);
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("请求失败");
        }
    }

    @RequestMapping("/groupHourForCountUser")
    public Object groupHourForCountUser(String startTime, String endTime, Integer siteId) {
        long allNewUserCount = xOtherSourceService.countUser(siteId.toString());
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = sf.parse(startTime);
            Date end = sf.parse(endTime);

            Map<String, Long> map = reportService.groupHourForActionByAction(DateUtil.getDayStartTime(start),
                    DateUtil.getDayEndTime(end), siteId, Action.FIRST_START);
            ListIterator<Map.Entry<String, Long>> li = new ArrayList<>(map.entrySet()).listIterator(map.size());
            while (li.hasPrevious()) {
                Map.Entry<String, Long> entry = li.previous();
                allNewUserCount -= entry.getValue();
                map.put(entry.getKey(), allNewUserCount);
            }
            return ResultJson.successResult("请求成功", map);
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("查询失败，cause by:" + e.getMessage());
            return ResultJson.errorMes("请求失败");
        }
    }
}
